package com.qf.fengmimall.product.controller;

import com.qf.fengmimall.ex.UserException;
import com.qf.fengmimall.product.service.IShoppingCartService;
import com.qf.fengmimall.vo.ResultVo;
import com.qf.fengmimall.vo.ShopCartVo;
import com.qf.fengmimall.vo.ShoppingCartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘欣
 * @createTime 2023/2/16 15:52
 * @description
 */
@RestController
@RequestMapping("/shopcart")
@CrossOrigin
@Slf4j
public class ProductShopCart {

    @Autowired
    private IShoppingCartService shoppingCartService;


    @RequestMapping("/add")
    public ResultVo add(@RequestBody ShopCartVo shopCartVo) throws UserException {
        log.debug("接受到的参数为shopCartVo={0}", shopCartVo);
        if (shopCartVo.getUserId() == null) {
            throw new UserException(20001, "用户没有登入");
        }
        shoppingCartService.insertShoppingCart(shopCartVo);

        return ResultVo.ok();
    }

    @RequestMapping("/list")
    public ResultVo list(String userId) {

        List<ShoppingCartVo> vo = shoppingCartService.shoppingCartList(userId);

        return ResultVo.ok(vo);
    }

    /**
     * 更改购物车数量
     *
     * @param id  购物车主键
     * @param num 更改数量
     * @return
     */
    @PutMapping("/update/{id}/{num}")
    public ResultVo update(@PathVariable("id") Integer id, @PathVariable("num") String num) {
        shoppingCartService.updateShoppCart(id, num);
        return ResultVo.ok();
    }

    /**
     * 查询购物车信息
     *
     * @param cids
     * @return
     */
    @RequestMapping("/listbycids")
    public ResultVo listByCids(String cids) {
        log.debug("接受到的参数cids={0}", cids);
        List<ShoppingCartVo> vo = shoppingCartService.listByCids(cids);
        return ResultVo.ok(vo);
    }
}
